﻿using AdverseEventReportSystem.BLL.NursingWorkHomeService;
using AdverseEventReportSystem.Core;
using AdverseEventReportSystem.Utils;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;

namespace AdverseEventReportSystem.Web.Controllers
{
    public class MainController : BaseController
    {
        NursingWorkHomeSoapClient service = new NursingWorkHomeSoapClient();
        //
        // GET: /Main/
        public ActionResult Index()
        {
            DataTable dt1 = null;
            //获取患者总览 患者总数、特级、一级、二级、入出人数、病危、病重
            var ret1 = service.GetPatientTodaySumInfo();
            if (ret1.S)
            {
                dt1 = JsonHelper.ToDataTable(ret1.D);
            }
            ViewBag.DT1 = dt1;
            //护士总览
            DataTable dt2 = null;
            var ret2 = service.GetNurseSumInfo();
            if (ret2.S)
            {
                dt2 = JsonHelper.ToDataTable(ret2.D);
            }
            ViewBag.DT2 = dt2;

            //病区有多少患者和护士
            DataTable dtbqhzhs = null;
            var retbqhzhs = service.GetWardPatientAndNurse();
            if (retbqhzhs.S)
            {
                dtbqhzhs = JsonHelper.ToDataTable(retbqhzhs.D);
            }
            ViewBag.DTBQHZHS = dtbqhzhs;

            //特殊人群
            DataTable dtts = null;
            var retts = service.GetSpecialGroupSumInfo();
            if (retts.S)
            {
                dtts = JsonHelper.ToDataTable(retts.D);
            }
            ViewBag.DTTS = dtts;
            //风险评估
            DataTable dtfxpg = null;
            var retfxpg = service.GetRiskLevelSumInfo();
            if (retfxpg.S)
            {
                dtfxpg = JsonHelper.ToDataTable(retfxpg.D);
            }
            ViewBag.DTFXPG = dtfxpg;
            //用药方法
            DataTable dtyyff = null;
            var retyyff = service.GetRecipeSumInfo();
            if (retyyff.S)
            {
                dtyyff = JsonHelper.ToDataTable(retyyff.D);
            }
            ViewBag.DTYYFF = dtyyff;
            return View();
        }

        /// <summary>
        /// 获取患者汇总信息
        /// </summary>
        /// <returns></returns>
        public ActionResult GetPatientHistorySumInfo()
        {
            OperateResult resultInfo = new OperateResult();
            try
            {
                var rethzhz = service.GetPatientHistorySumInfo();
                if (rethzhz.S)
                {
                    resultInfo.Body = JsonHelper.ToDataTable(rethzhz.D);
                }
                else
                {
                    resultInfo.Result = 0;
                    resultInfo.Body = rethzhz.M;
                }
            }
            catch (Exception ex)
            {
                resultInfo.Result = -1;
                resultInfo.Body = ex.Message;
            }
            return JsonDate(resultInfo); ;
        }

        public ActionResult GetRiskLevelGroupInfo()
        {
            int dataType = Convert.ToInt32(Request["datetype"] ?? "0");
            int formId = Convert.ToInt32(Request["formid"] ?? "0");
            string json1 = ("{ \"data_pie\":[");
            string json3 = ("] }");
            OperateResult resultInfo = new OperateResult();
            try
            {
                var rethzhz = service.GetRiskLevelGroupInfo(dataType, formId);
                if (rethzhz.S)
                {
                    DataTable dt = JsonHelper.ToDataTable(rethzhz.D);                    
                    StringBuilder json = new StringBuilder();
                    for (int i = 0; i < dt.Rows.Count; i++)			 
                    {
                        string num = dt.Rows[i]["NUM"].ToString();
                        if (string.IsNullOrEmpty(num)) num = "1";
                        string itemname = dt.Rows[i]["ITEM_NAME"].ToString();
                        if (string.IsNullOrEmpty(itemname)) itemname = "无标题";
                        json.Append("{\"name\":\"").Append(itemname).Append("：").Append(num).Append("\",\"value\":").Append(num).Append("},");
                    }
                    string json2 = json.ToString();
                    if (json2.Length > 0) json2 = json2.Substring(0, json2.Length - 1);
                    else json2 = "{\"value\":1, \"name\":\"暂无数据\"}";
                    return Content(json1 + json2 + json3);
                }
                else
                {
                   return Content(json1 +"{\"value\":1, \"name\":\"暂无数据\"}"+ json3);
                }
            }
            catch(Exception ex)
            {
                return Content(json1 +"{\"value\":1, \"name\":\"暂无数据\"}"+ json3);
            }
        }
    }
}